package com.android.ddfafu.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.ddfafu.R;
import com.android.ddfafu.domian.ResultInfo;
import com.android.ddfafu.utils.Const;
import com.android.ddfafu.utils.UserHttpUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Register_activity extends AppCompatActivity {

    private SharedPreferences sp_user;
    private EditText username;
    private EditText password;
    private EditText name;
    private Button register;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //绑定账号输入框
        username = findViewById(R.id.loginusername);
        //绑定密码输入框
        password = findViewById(R.id.loginpassword);
        //绑定用户名输入框
        name = findViewById(R.id.registername);
        //绑定注册按钮
        register = findViewById(R.id.registbutton);

        sp_user = Register_activity.this.getSharedPreferences("userData", Context.MODE_PRIVATE);
        register.setOnClickListener(v -> {
            String registerUserccount = username.getText().toString();
            String registerPassword = password.getText().toString();
            String registerName = name.getText().toString();
            if (StringUtils.isEmpty(registerUserccount)){
                username.setHint("请输入邮箱");
                username.setHintTextColor(getResources().getColor(R.color.red));
                return;
            }else {
                if (!registerUserccount.matches(Const.EMAIL_PATTERN)){
                    username.setText("");
                    username.setHint("邮箱格式错误");
                    username.setHintTextColor(getResources().getColor(R.color.red));
                    return;
                }
            }

            if (StringUtils.isEmpty(registerPassword)){
                password.setHint("请输入密码");
                password.setHintTextColor(getResources().getColor(R.color.red));
                return;
            }
            if (StringUtils.isEmpty(registerName)){
                name.setHint("请输入昵称");
                password.setHintTextColor(getResources().getColor(R.color.red));
                return;
            }
            registerWithOkHttp(Const.SERVER_URL_REGISTER,registerUserccount,registerPassword,registerName);

            Intent intent = new Intent(this,checkemail_activity.class);
            intent.putExtra("username",registerUserccount);
            startActivity(intent);
        }) ;


        //点击返回按钮跳转到登录页面
        Button to_login = findViewById(R.id.to_login);
        to_login.setOnClickListener(v -> {
            Intent i = new Intent(Register_activity.this, Login_activity.class);
            // 启动
            startActivity(i);
            overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
        });
    }


    //实现注册
    public void registerWithOkHttp(String url,String username,String password,String name){
        UserHttpUtil.registerWithOkHttp(url, username,password,name,new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //在这里对异常情况进行处理
                Toast.makeText(Register_activity.this,"服务器错误",Toast.LENGTH_SHORT).show();
                Log.i("onFailure","onFailure");
                Log.i("onFailure",e.getMessage());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.i("onResponse","onResponse");
                final String responseData = response.body().string();
                ObjectMapper mapper =new ObjectMapper();
                final ResultInfo info = mapper.readValue(responseData,ResultInfo.class);
                runOnUiThread(() -> {
                    if ( info.isFlag() ){
                        //Intent i = new Intent(Register_activity.this, Login_activity.class);
                        if (sp_user!=null){
                            SharedPreferences.Editor edit = sp_user.edit();
                            edit.putString("USERNAME","");
                            edit.putString("PASSWORD","");
                            edit.putString("TOKEN","");
                            edit.putString("NAME","");
                            edit.putBoolean("REMEMBER",false);
                            edit.apply();
                            edit.commit();
                        }
                        // 启动
                        //startActivity(i);
                        Toast.makeText(Register_activity.this,"注册成功,请到邮箱点击链接激活",Toast.LENGTH_SHORT).show();
                        overridePendingTransition(R.anim.zoomout, R.anim.zoomin);
                    }else{
                        Toast.makeText(Register_activity.this,"注册失败,"+info.getMsg(),Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}
